<style type="text/css">
    .admin_role_edit td{padding:8px 0 !important;}
    .admin_role_edit .children_td span{color:#adadad;margin-left:20px;}
    .admin_role_edit .children_td .ilabel{color:#ea5e00;}
    .admin_role_edit .check_son_div .ilabel{color:#464646;margin-right:40px;}
    .admin_role_edit .check_son_div{width:100%;padding-top:10px;}
    .admin_role_edit .check_son_div label{font-weight:normal !important;}
</style>

<div class="bjui-pageContent">
	<div><h4><?php echo Iif($role['id'], '编辑角色', '添加角色');?></h4></div>
    <form action="<?=admin_url('role/save');?>" class="pageForm" data-toggle="validate" name="admin_role_edit" id="admin_role_edit">
        <input type="hidden" name="id" value="<?=$role['id'];?>">
        <table class="table table-condensed table-hover admin_role_edit">
            <tbody>
                <tr>
                    <td>
                        <label class="control-label x85">角色名称：</label>
                    </td>
                    <td width="100%">
						<input class="form-control" type="text" name="role_name" value="<?=$role['role_name'];?>" data-rule="required" size="26">
                    </td>
                </tr>

				<?php if(!empty($role['id'])):?>
                <tr>
                    <td>
                        <label class="control-label x85">状态：</label>
                    </td>
                    <td width="100%">
						<input type="radio" name="status" value="1" id="status-1" data-toggle="icheck" data-label="正常" <?php if($role['status'] == '1'):?> checked<?php endif;?>>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
						<input type="radio" name="status" value="2" id="status-2" data-toggle="icheck" data-label="禁用" <?php if($role['status'] == '2'):?> checked<?php endif;?>>
						<span style="color:red;vertial-align:middle;">(注：禁用后属于此角色的用户将无法登录)</span>
                    </td>
                </tr>
				<?php endif;?>

				<?php foreach ($admin_resources as $k => $resource):?>
				<?php $_resources = unserialize($resource->resources);?>
                <tr>
                    <td style="vertical-align:top;">
                        <label class="control-label x85 fade"><?=($k +1);?>：</label>
                    </td>
                    <td width="100%" class="children_td">

						<?php if(!empty($role['id']) AND array_key_exists($resource->file_path, $permissions)):?>

						<input type="checkbox" name="parent_resources[]" id="parent_<?=$resource->id;?>" value="<?=$resource->id;?>||<?=$resource->file_path;?>" data-id="<?=$resource->id;?>" data-toggle="icheck" data-label="<?=$resource->source_name;?>" checked>
						<span><?=$resource->file_path;?></span>
						<div class="check_son_div" id="check_son_div_<?=$resource->id;?>">

							<?php foreach ($_resources as $key => $val):?>
							<input type="checkbox" name="son_resources_<?=$resource->id;?>[]" id="son_<?=$resource->id;?>_<?=$key;?>" value="<?=$key;?>" data-pid="<?=$resource->id;?>" data-toggle="icheck" data-label="<?=$val;?>" <?php if(in_array($key, $permissions[$resource->file_path])):?> checked<?php endif;?>>
							<?php endforeach;?>

						</div>

						<?php else:?>

						<input type="checkbox" name="parent_resources[]" id="parent_<?=$resource->id;?>" value="<?=$resource->id;?>||<?=$resource->file_path;?>" data-id="<?=$resource->id;?>" data-toggle="icheck" data-label="<?=$resource->source_name;?>">
						<span><?=$resource->file_path;?></span>
						<div class="check_son_div" id="check_son_div_<?=$resource->id;?>">

							<?php foreach ($_resources as $key => $val):?>
							<input type="checkbox" name="son_resources_<?=$resource->id;?>[]" id="son_<?=$resource->id;?>_<?=$key;?>" value="<?=$key;?>" data-pid="<?=$resource->id;?>" data-toggle="icheck" data-label="<?=$val;?>">
							<?php endforeach;?>

						</div>

						<?php endif;?>
                    </td>
                </tr>
				<?php endforeach;?>

            </tbody>
        </table>
    </form>
</div>
<div class="bjui-pageFooter">
    <ul>
        <li><button type="button" class="btn-close">关闭</button></li>
        <li><button type="submit" class="btn-green">保存</button></li>
    </ul>
</div>

<script type="text/javascript">
$(function() {
    $.CurrentDialog.find('.check_son_div input').on('ifChanged', function(e) {
        var checked = $(this).is(':checked');
		var pid = $(this).data("pid");
         
        if (checked){

			$.CurrentDialog.find("#parent_" + pid).iCheck('check'); 

        }else{
			var parent_unchecked = true;

           $.CurrentDialog.find('#check_son_div_'  + pid + ' input[type="checkbox"]').each(function() {
				var chked = $(this).is(':checked');

				if(chked){
					parent_unchecked = false;
					return false;
				}
			});

			if(parent_unchecked){
				$.CurrentDialog.find("#parent_" + pid).iCheck('uncheck');
			}
		}
    });

    $.CurrentDialog.find("input[id^='parent_']").on('ifClicked', function(e) {
        var checked = $(this).is(':checked');
		var id = $(this).data("id");

        if (checked){
			$.CurrentDialog.find('#check_son_div_'  + id + ' input[type="checkbox"]').each(function() {
				$(this).iCheck('uncheck');
			});

        }else{
			$.CurrentDialog.find('#check_son_div_'  + id + ' input[type="checkbox"]').each(function() {
				$(this).iCheck('check');
			});
		}
    });
});

</script>
